<?php

class Admin_IndexController extends Zend_Controller_Action {

    public function init() {
        /* Initialize action controller here */
    }

    public function indexAction() {

        $dictionary = array('en' => array(), 'ar' => array());
        $dictionary['en']['home'] = unserialize(file_get_contents(ROOT_PATH . '/data/lang/home-en'));
        $dictionary['en']['about'] = unserialize(file_get_contents(ROOT_PATH . '/data/lang/about-en'));
        $dictionary['en']['contact'] = unserialize(file_get_contents(ROOT_PATH . '/data/lang/contact-en'));
        $dictionary['ar']['home'] = unserialize(file_get_contents(ROOT_PATH . '/data/lang/home-ar'));
        $dictionary['ar']['about'] = unserialize(file_get_contents(ROOT_PATH . '/data/lang/about-ar'));
        $dictionary['ar']['contact'] = unserialize(file_get_contents(ROOT_PATH . '/data/lang/contact-ar'));
        $this->view->dic = $dictionary;
    }

    public function contentupdateAction() {
        $res = $this->_request->getPost();

        $en = array();
        $ar = array();
        $controller = $res['controller'];
        unset($res['controller']);
        foreach ($res as $key => $v) {
            if ($key == 'News1-day' || $key == 'News2-day' || $key == 'News1-month' || $key == 'News2-month' || $key == 'Info-phone' || $key == 'Info-mail') {
                
                $en[$key] = urldecode($v);
                $ar[$key] = urldecode($v);
                continue;
            }
            if (preg_match("/[^Clear]ar$/i", $key)) {
                $ar[substr($key, 0, -3)] = urldecode($v);
            } else {
                if ($key != 'save' && $key != 'clear') {
                    $en[substr($key, 0, -3)] = urldecode($v);
                }
            }
        }

        $dictionary['en'][$controller] = unserialize(file_get_contents(ROOT_PATH . '/data/lang/' . $controller . '-en'));
        $dictionary['ar'][$controller] = unserialize(file_get_contents(ROOT_PATH . '/data/lang/' . $controller . '-ar'));

        echo exec('cp ' . ROOT_PATH . '/data/lang/' . $controller . '-en ' . ROOT_PATH . '/data/lang/bak/' . $controller . '-en-' . time());
        echo exec('cp ' . ROOT_PATH . '/data/lang/' . $controller . '-ar ' . ROOT_PATH . '/data/lang/bak/' . $controller . '-ar-' . time());

        $dictionary['en'][$controller] = array_replace($dictionary['en'][$controller], $en);
        $dictionary['ar'][$controller] = array_replace($dictionary['ar'][$controller], $ar);
//        var_dump($ar['Vision-content']==$dictionary['ar'][$controller]['Vision-content']);
        file_put_contents(ROOT_PATH . '/data/lang/' . $controller . '-en', serialize($dictionary['en'][$controller]));
        file_put_contents(ROOT_PATH . '/data/lang/' . $controller . '-ar', serialize($dictionary['ar'][$controller]));

        $this->_helper->layout()->disableLayout();
        $this->_helper->viewRenderer->setNoRender(true);
    }

    public function loginAction() {
        $this->_helper->layout->disableLayout();
        $form = new Golib_Form_Login();
        $form->setAction('/admin/index/login');
        $this->view->form = $form;
        
        if ($this->_request->isPost()) {
            if ($form->isValid($_POST)) {
                $auth = Zend_Auth::getInstance();
                $result = $auth->authenticate(new Golib_Auth_SimpleAdapter($_POST['username'], $_POST['password']));
                switch ($result->getCode()) {
                    case Zend_Auth_Result::FAILURE_IDENTITY_NOT_FOUND:
                        $this->view->message = '<font color="red">user name is unvalid</font>';
                        break;
                    case Zend_Auth_Result::FAILURE_CREDENTIAL_INVALID:
                        $this->view->message = '<font color="red">unvalid password provided</font>';
                        break;
                    default:
                        $session = new Zend_Session_Namespace('Default');
                        $session->status = $auth->getIdentity();
                        $this->_redirect('/admin');
                        break;
                }
                
            }
        }
    }

        public function logoutAction() {
        $session = new Zend_Session_Namespace('Default');
        $session->unsetAll();
        $this->_redirect('/admin/index/login');
        }
    
    
    /*
     * 
     * THIS UPDATE NAME ONLY
     * STOP WORK TILL SEE IF IT REQUIRED
     * 
     * 
     */

    public function updateAction() {
        $this->_helper->viewRenderer('add');
        $form = new Form_Audio();
        $update = $form->getElement('submit');
        $update->setLabel('تعديل');
        if ($this->_request->isGet() && $_GET['id']) {
            $audio = MediaTable::getInstance()->findOneBy('id', $_GET['id']);
            $elmId = new Zend_Form_Element_Hidden('id');
            $elmId->setValue($_GET['id']);
            $form->addElement($elmId);
        } elseif ($this->_request->isPost()) {
//            var_dump($_POST);die();
            $elmId = new Zend_Form_Element_Hidden('id');
            $elmId->setValue($_POST['id']);
            $form->addElement($elmId);
            $audio = MediaTable::getInstance()->findOneBy('id', $_POST['id']);
            $updated = false;
            if ($audio->title != $_POST['name']) {
                $audio->title = $_POST['name'];
                $updated = true;
            }

            if ($updated)
                $audio->replace();
        }
//        var_dump($audio);die();
        $form->getElement('name')->setValue($audio->title);
//        var_dump($audio->level);die();
        $form->getElement('auther')->setValue($audio->Auther['name']);
        $form->getElement('category')->setValue($audio->Categories['name']);
        $form->getElement('level')->setValue($audio->Levels['number']);
        $form->removeElement('file');

        $this->view->form = $form;
    }

    public function deleteAction() {
        var_dump($_POST);
        die();
//        $audio=new Media();
//        $audio->assignIdentifier($_GET['id']);
//        $audio->delete();
//        $this->_redirect('/admin');        
    }

}

